package com.inspur.gs.sbssdl.sbyxgl.gzjl.gzjl.back.bizactions;

import com.inspur.edp.bef.api.be.IBEManagerContext;
import com.inspur.edp.bef.api.be.IBEService;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.cdp.coderule.api.CodeRuleBEAssignService;
import com.inspur.edp.cef.entity.changeset.ModifyChangeDetail;
import com.inspur.edp.cef.entity.changeset.ValueObjModifyChangeDetail;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import lombok.extern.slf4j.Slf4j;


/**
 * 故障单审批成功回填设备状态
 */
@Slf4j
public class sbssGzjlWriteBackStatusMgrAction extends AbstractManagerAction<String> {
    private String dataid;
    private String ryid;
    private String jlz;

    public sbssGzjlWriteBackStatusMgrAction(IBEManagerContext managerContext,
                                            String dataid, String ryid, String jlz) {
        super(managerContext);
        this.dataid = dataid;
        this.ryid = ryid;
        this.jlz = jlz;
    }

    //BE的configId组合
    private static class ConfigID {
        public static final String sbtzconfigID = "efd56b39-0e8d-4441-9fa1-2b019b6fd862";  //设备台账
        public static final String faultConfigID = "36d553b0-e4f7-4aeb-bdf9-c747396ab5a0"; //故障记录
        public static final String sbbxConfigID = "c5fd08f7-6410-4d7c-b662-693f94fde569"; //设备报修
        public static final String wxjlConfigID = "3d097fda-c626-461b-81a9-e6040eba0270"; //维修记录

    }

    //编码规则组合
    private static class BMGZ {
        public static final String SBBX_BMGZ = "874c20de-d4e2-4447-9b79-e1fff48318f9"; //设备报修编码规则
        public static final String WXJL_BMGZ = "d0083c82-102d-4be6-9a6b-42768faa5a6b";  //维修记录编码规则
    }

    private final CodeRuleBEAssignService rule = SpringBeanUtils.getBean(CodeRuleBEAssignService.class);

    @Override
    public void execute() {
        if (jlz.isEmpty() || jlz.equals("Back")) {
            return;
        }
        SpringBeanUtils.getBean(IBefSessionManager.class).createSession();
        try {
            ILcpFactory iLcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
            IStandardLcp faultLcp = iLcpFactory.createLcpByBEId(ConfigID.faultConfigID);
            IEntityData faultData = faultLcp.retrieve(dataid).getData(); //故障单
            IStandardLcp sbtzLcp = iLcpFactory.createLcpByBEId(ConfigID.sbtzconfigID);

            //获取故障单内容
            AssociationInfo sbidInfo = (AssociationInfo) faultData.getValue("Sbid");
            String sbid = sbidInfo.getValue("Sbid").toString(); //获取设备id
            String gzdh = faultData.getValue("Gzdh").toString();
            String gzlx = (String) ((IValuesContainer) faultData.getValue("Gzfl")).getValue("Gzlb_Gzfle");

            //获取台账内容
            IEntityData sbtz = sbtzLcp.retrieve(sbid).getData(); //获取台账信息
            String sbbh = sbtz.getValue("sbbh").toString(); //设备编号
            String sbmc = sbtz.getValue("sbmc").toString(); //设备名称
            String gzny = sbtz.getValue("rzrq") != null ? sbtz.getValue("rzrq").toString() : "";
            String cq = (String) ((IValuesContainer) sbtz.getValue("shop_id")).getValue("shop_id_ShopName");//厂区
            String sybm = (String) ((IValuesContainer) sbtz.getValue("bmbh_id")).getValue("bmbh_id_name"); //部门名称
            String ggxh = sbtz.getValue("ggxh") != null ? sbtz.getValue("ggxh").toString() : "";
            String xh = sbtz.getValue("xh") != null ? sbtz.getValue("xh").toString() : "";
            String lbmc = (String) ((IValuesContainer) sbtz.getValue("sblb_id")).getValue("sblb_id_lbmc"); //类别名称
            String ztmc = (String) ((IValuesContainer) sbtz.getValue("zt")).getValue("zt_ztmc"); //状态名称
            String abcmc = (String) ((IValuesContainer) sbtz.getValue("abc")).getValue("abc_lxmc"); //abc名称
            String sbbhOld = sbtz.getValue("sbbh_old").toString(); //设备编号

            //主表变更
            ModifyChangeDetail change = new ModifyChangeDetail(sbtz.getID());
            AssociationInfo ztInfo = new AssociationInfo();
            ztInfo.setValue("zt", "7a16e6bb-84f8-4a95-89ec-b54983582ec5");
            change.getPropertyChanges().put("zt", ztInfo);  //将使用状态改为故障

            //子表变更
            ValueObjModifyChangeDetail bemodify = new ValueObjModifyChangeDetail();
            bemodify.setItem("updValue", "故障");
            bemodify.setItem("changeUser", ryid);
            bemodify.setItem("valueFrom", "故障维修" + gzdh);
            sbtzLcp.modifyVariable(bemodify);

            sbtzLcp.modify(change);
            sbtzLcp.save();

            String sbbxlsh = getLsh(iLcpFactory, ConfigID.sbbxConfigID, BMGZ.SBBX_BMGZ);
            String wxjllsh = getLsh(iLcpFactory, ConfigID.wxjlConfigID, BMGZ.WXJL_BMGZ);


            //生成保修单
            IStandardLcp sbbxlcp = iLcpFactory.createLcpByBEId(ConfigID.sbbxConfigID);
            IEntityData newsbbxData = sbbxlcp.retrieveDefault();
            ModifyChangeDetail sbbxAdd = new ModifyChangeDetail(newsbbxData.getID());
            sbbxAdd.getPropertyChanges().put("lsh", sbbxlsh);
            sbbxAdd.getPropertyChanges().put("gzdbh", gzdh);
            sbbxAdd.getPropertyChanges().put("gzlx", gzlx);
            sbbxAdd.getPropertyChanges().put("sbbh", sbbh);
            sbbxAdd.getPropertyChanges().put("sbmc", sbmc);
            sbbxAdd.getPropertyChanges().put("gzny", gzny);
            sbbxAdd.getPropertyChanges().put("cq", cq);
            sbbxAdd.getPropertyChanges().put("sybm", sybm);
            sbbxlcp.modify(sbbxAdd);
            sbbxlcp.save();

            //生成维修单
            IStandardLcp wxjllcp = iLcpFactory.createLcpByBEId(ConfigID.wxjlConfigID);
            IEntityData newwxjlData = wxjllcp.retrieveDefault();
            ModifyChangeDetail wxjlAdd = new ModifyChangeDetail(newwxjlData.getID());
            wxjlAdd.getPropertyChanges().put("wxdh", wxjllsh);
            AssociationInfo gzdhInfo = new AssociationInfo();
            gzdhInfo.setValue("gzdh", dataid);
            wxjlAdd.getPropertyChanges().put("gzdh", gzdhInfo);  //故障单号
            wxjlAdd.getPropertyChanges().put("sbbh", sbbh);
            wxjlAdd.getPropertyChanges().put("sbmc", sbmc);
            wxjlAdd.getPropertyChanges().put("ggxh", ggxh);
            wxjlAdd.getPropertyChanges().put("xh", xh);
            wxjlAdd.getPropertyChanges().put("shopName", cq);
            wxjlAdd.getPropertyChanges().put("sbbh_old", sbbhOld);
            wxjlAdd.getPropertyChanges().put("lbmc", lbmc);
            wxjlAdd.getPropertyChanges().put("ztmc", ztmc);
            wxjlAdd.getPropertyChanges().put("abcmc", abcmc);
            wxjlAdd.getPropertyChanges().put("bmmc", sybm);
            wxjllcp.modify(wxjlAdd);
            wxjllcp.save();

        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }

    }


    /**
     * @return 生成的编号
     */
    private String getLsh(ILcpFactory iLcpFactory, String beId, String RuleId) {
        IStandardLcp enLcp = iLcpFactory.createLcpByBEId(beId);
        IEntityData entityData = enLcp.retrieveDefault();
        enLcp.delete(entityData.getID());
        return rule.generate(beId, "", entityData, RuleId);
    }


    private IBusinessEntity getEntity(String dataId) {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(dataId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }


}